//
//  NewsVCDetailWebView.m
//  SpiritWalk
//
//  Created by shange1999 on 16/3/14.
//  Copyright © 2016年 王鑫. All rights reserved.
//

#import "NewsVCDetailWebView.h"

@interface NewsVCDetailWebView ()<UIWebViewDelegate>
@property(nonatomic,retain)UIWebView *webview;



@end

@implementation NewsVCDetailWebView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    NSString *receiveId =[NSString stringWithFormat:@"%@",self.receiveID];
    NSString *urlStr =[NSString stringWithFormat:@"%@%@%@",@"http://m.ys137.com/article_",receiveId,@".html?from=app_android&action="];
    NSURL *url=[NSURL URLWithString:urlStr];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    
     _webview =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH-10, HEIGHT-64)];
     [_webview loadHTMLString:@"<p><body></p>" baseURL:nil];
    _webview.userInteractionEnabled=YES;
    _webview.scalesPageToFit=YES;
    [self.view addSubview:_webview];
  
    _webview.delegate=self;
    
       [_webview loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('/head')[0].style.display = 'NONE'"];
//     [webView stringByEvaluatingJavaScriptFromString:@"javascript:document.getElementById('content=').style.display='none';"];
    

}
- (void)showNews:(NSDictionary *)news
{
    // 1.取出网页内容
    NSString *body = news[@"body"];
    
    // 2.取出图片
    NSDictionary *img = [news[@"img"] lastObject];
    NSString *imgHTML = [NSString stringWithFormat:@"<img src=\"%@\" width=\"300\" height=\"171\">", img[@"src"]];
    
    // 2.创建一个webView，显示网页
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = self.view.bounds;
    [self.view addSubview:webView];
    
    // 3.拼接网页内容
    NSString *html = [body stringByReplacingOccurrencesOfString:img[@"ref"] withString:imgHTML];
    
    // 4.取出新闻标题
    NSString *title = news[@"title"];
    // 5.取出新闻的时间
    NSString *time = news[@"ptime"];
    
    // 头部内容
    NSString *header = [NSString stringWithFormat:@"<div class=\"title\">%@</div><div class=\"time\">%@</div>", title, time];
    html = [NSString stringWithFormat:@"%@%@", header, html];
    
    // 链接mainBundle中的CSS文件
    NSURL *cssURL = [[NSBundle mainBundle] URLForResource:@"news" withExtension:@"css"];
    html = [NSString stringWithFormat:@"%@<link rel=\"stylesheet\" href=\"%@\">", html, cssURL];
    
    // 5.加载网页内容
    [webView loadHTMLString:html baseURL:nil];
}

// 方法三：
NSString *str = @"meta http-equiv="@"X-UA-Compatible" @"content="@"IE=edge";
- (NSString *)transformMessage:(NSString *)message withString:(NSString *)string
{
    NSString *messageCopy = message;
    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString:messageCopy];
    while ([theScanner isAtEnd] == NO)
    {
        // find start of tag
        [theScanner scanUpToString:@".jpg" intoString:NULL] ;
        // find end of tag
        if ([theScanner scanUpToString:@">" intoString:&text] || [theScanner scanUpToString:@"/>" intoString:&text]) {
            // .jpg" height="200" width="340"/>
            // 如果不需要第二个参数 ：
            // 可以将string替换为 [NSString stringWithFormat:@".jpg\" height=\\\"200\" width=\"340\"/>"]
            if (text != nil) {
                messageCopy = [messageCopy stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:string];
                NSLog(@"%@",messageCopy);
                break;
            }
        }
    }
    return messageCopy;
}


















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
